/**
 * 
 */
package myLucene;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

/**
 * @author J.Feit 30.11.2010 14:49:46
 */
public class Decompress {

	/**
	 * @throws IOException
	 * @throws FileNotFoundException
	 * 
	 */

	static StringBuffer sb = new StringBuffer();

	// public Decompress(String File) {
	// 
	// }

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		String path = "//proj/corpora/Spiegel";
		// String path ="/proj/idix/joofeit/dewac";
		searchFolders(path);
		System.out.println("!!!aaaaaawwwwwwwwwwwwwwwwwwwwwwwww");
	}

	public static void decompress(String path) throws FileNotFoundException,
			IOException {
		// public static void main(String[] args) throws FileNotFoundException,
		// IOException{
		// GZIPInputStream ginput= new GZIPInputStream(new FileInputStream(new
		// File("//proj/corpora/Spiegel/spiegel_1980_01.tts.out.gz")));
		GZIPInputStream ginput = new GZIPInputStream(new FileInputStream(
				new File(path)));

		InputStreamReader ireader = new InputStreamReader(ginput, "iso-8859-1");
		System.out.println("Encoding: " + ireader.getEncoding());

		BufferedReader breader = new BufferedReader(ireader);

		String line = new String();

		sb
				.append("!!!!!!!!!!!!!!!!!!!!: "
						+ path
						+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		while ((line = breader.readLine()) != null) {
			if (!line.startsWith("<")) {
				String newl = line.replaceAll("\\s+", " ");
				// System.out.println(newl);
				String[] lineSplit = newl.split(" ");
				if (lineSplit.length > 2) {
					sb.append(lineSplit[0] + " ");
				}
			}
		}
		sb
				.append("!!!!!!!!!!!!!!!!!!!!: "
						+ path
						+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		breader.close();

		// System.out.println(sb.toString());
	}

	public static void searchFolders(String path) throws FileNotFoundException,
			IOException {
		File file = new File(path);
		if (file.isDirectory()) {
			String[] fileList = file.list();
			for (String s : fileList) {
				System.out.println("File :" + path + "/" + s);
				if (s.endsWith("gz")) {
					decompress(path + "/" + s);
				}

			}
			System.out.println(sb.toString());
		}

	}
}
